package com.example.zbh.dsliao.ui.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.ContactPeople;
import com.example.zbh.dsliao.ui.activty.BaseActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zbh on 15/11/23.
 */
public class GroupPickContactAdapter extends BaseAdapter {
    private List<ContactPeople> list;
    private List<String> listName = new ArrayList<>();

    public void addData(List<ContactPeople> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public boolean isLength() {
        return list != null && list.size() > 0;
    }
    public List<String> getAddContactName(){
        return listName;
    }

    @Override
    public int getCount() {
        return isLength() ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        View view = null;
        if (convertView == null){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contactlist_listview, null);
        }else {
            view = convertView;
        }
        ViewHolder holder = ViewHolder.getHolder(view);
        final ContactPeople people = list.get(position);
        String currentLetter = people.getPinyin().charAt(0) + "";
        String indexStr = null;
        if (position == 0){
            indexStr = currentLetter;
        }else {
            String preLetter = list.get(position - 1).getPinyin().charAt(0) + "";
            if (!TextUtils.equals(currentLetter, preLetter)){
                indexStr = currentLetter;
            }
        }
        holder.tv_index.setVisibility(indexStr == null ? View.GONE : View.VISIBLE);
        holder.tv_index.setText(indexStr);
        holder.tv_name.setText(people.getName());
        holder.checkBox.setVisibility(View.VISIBLE);

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int isChange = people.getIsChange();
                if (isChecked){
                 if (isChange == 0){
                     isChange ++ ;
                     people.setIsChange(isChange);
                     listName.add(people.getName());
                    Log.i("geanwen", people.getName() + listName.size());
                 }
                }else {
                    if (isChange ==1){
                        isChange --;
                        people.setIsChange(isChange);
                        listName.remove(people.getName());
                    Log.i("geanwen","删除" + people.getName() + listName.size());
                    }
                }
            }
        });
        if (people.getIsChange() == 1){
            holder.checkBox.setChecked(true);
        }else {
            holder.checkBox.setChecked(false);
        }
        return view;
    }


    static class ViewHolder {
        private TextView tv_index, tv_name;
        private CheckBox checkBox;

        public static ViewHolder getHolder(View view) {
            ViewHolder holder = (ViewHolder) view.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                holder.tv_index = (TextView) view.findViewById(R.id.tv_index);
                holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
                holder.checkBox = (CheckBox) view.findViewById(R.id.cb_public);
            }
            return holder;
        }
    }


}
